jQuery(document).ready(function() {
   quitarCss();
   $.validator.messages.required = '*';
   $.validator.messages.email = 'Please enter a valid email';
   $.validator.messages.number = 'Please enter a valid number';

   inicializarTodaFecha();
});

//function inicializarTodaFecha(){
//    $(".datepicker").each(function(index,datepicker){
//        $(datepicker).datepicker( "option", "dateFormat", 'dd-mmm-yy' );
//        var date = new Date($(datepicker).val());
//        $(datepicker).val(date.format('dd-mmm-yy'));
//  });
//}

function inicializarTodaFecha(){
    $(".datepicker").each(function(i,v){
        var idDate = "#" + v.id;
        var idAlt = "#alt" + v.id;
        initializeDate(idDate, idAlt);
    });
}

function initializeDate(idDate, idAlt){
    $(idDate).datepicker({
        altField : idAlt,
        altFormat: 'dd/mm/y',
        dateFormat: 'dd/mm/y'
    });

    $(idDate).val($(idAlt).val());
    $(idDate).datepicker( "option", "dateFormat", 'dd/M/y' );
}

function setEmptyDates(){
    $(".datepicker").each(function(i,v){
        if($(v).val() == ''){
            var idAlt = "#alt" + v.id;
            $(idAlt).val('');
        }
    });
}

function showMoreInfo(div,className){
    div.dialog({
        autoOpen: false
    });

    $("." + className).hover(

        function() {
            div.dialog({
                closeText: "",
                draggable: false,
                resizable: false,
                height: 60
            }).siblings('div.ui-dialog-titlebar').remove();

            div.removeClass(className).dialog('open');
            var target = $(this);
            div.dialog("widget").position({
                my: 'left top',
                at: 'center bottom',
                of: target
            });
            div.siblings('div.ui-dialog-titlebar').remove();
        }, function() {
            div.siblings('div.ui-dialog-titlebar').remove();
            div.dialog('close');
            div.siblings('div.ui-dialog-titlebar').remove();
        });
}

/*$(function() {
    $(".datepicker").datepicker({ dateFormat: 'dd/M/y'});

});*/

function loadCollapse(ids){

    for(var i=0; i<ids.length ; i++){

        var collapse = new jQueryCollapse($("#"+ids[i][0]), {
            open: function() {
                this.slideDown(150);
            },
            close: function() {
                this.slideUp(150);
            }
        });
        if(ids[i][1] == true)
            collapse.open();
            $("#"+ids[i][0]).show();
    }
}

function windowCaller(title, actionUrl, data){
    $.ajax({
        traditional: true,
        type: "POST",
        async: false,
        data: data,
        url: actionUrl,
        success: function(result)
        {
            $.window({
                showModal: true,
                title: title,
                modalOpacity: 0,
                content: result
            });
        }
    });
}


function popupCaller(actionUrl){
    $.ajax({
        traditional: true,
        type: "POST",
        async: false,
        url: actionUrl,
        success: function(result)
        {
            $.window({
                showModal: true,
                modalOpacity: 0,
                content: result
            });
        }
    });
}

function quitarCss(){
    jQuery(".tdLabel").removeClass("tdLabel");
    jQuery(".checkboxErrorLabel").removeClass("checkboxErrorLabel");
    jQuery(".required").removeClass("required");
    jQuery(".wwFormTable").removeClass("wwFormTable");
    jQuery(".label").removeClass("label");
    jQuery(".errorLabel").removeClass("errorLabel");
    jQuery(".errorMessage").removeClass("errorMessage");
    jQuery(".checkboxLabel").removeClass("checkboxLabel");
    jQuery(".checkboxErrorLabel").removeClass("checkboxErrorLabel");

}

function paginar(rows){
    var maxRows = rows;
    $('.paginated-table').each(function() {
        var cTable = $(this);
        var cRows = cTable.find('tr:gt(0)');
        var cRowCount = cRows.size();

        if (cRowCount < maxRows) {
            return;
        }

        cRows.each(function(i) {
            $(this).find('td:first').text(function(j, val) {
                return (i + 1) + " - " + val;
            });
        });

        cRows.filter(':gt(' + (maxRows - 1) + ')').hide();

        var cPrev = $('.prev');
        var cNext = $('.next');

        cPrev.addClass('disabled');

        cPrev.click(function() {
            var cFirstVisible = cRows.index(cRows.filter(':visible'));

            if (cPrev.hasClass('disabled')) {
                return false;
            }

            cRows.hide();
            if (cFirstVisible - maxRows - 1 > 0) {
                cRows.filter(':lt(' + cFirstVisible + '):gt(' + (cFirstVisible - maxRows - 1) + ')').show();
            } else {
                cRows.filter(':lt(' + cFirstVisible + ')').show();
            }

            if (cFirstVisible - maxRows <= 0) {
                cPrev.addClass('disabled');
            }

            cNext.removeClass('disabled');

            return false;
        });

        cNext.click(function() {
            var cFirstVisible = cRows.index(cRows.filter(':visible'));

            if (cNext.hasClass('disabled')) {
                return false;
            }

            cRows.hide();
            cRows.filter(':lt(' + (cFirstVisible +2 * maxRows) + '):gt(' + (cFirstVisible + maxRows - 1) + ')').show();

            if (cFirstVisible + 2 * maxRows >= cRows.size()) {
                cNext.addClass('disabled');
            }

            cPrev.removeClass('disabled');

            return false;
        });

    });
}

function selectItemMenu(item){
    var id = "#"
    if( $("#display").val() == 'ARGENTINA')
        id += "ar_";
    if( $("#display").val() == 'COLOMBIA')
        id += "co_";
    if( $("#display").val() == 'BRAZIL')
        id += "br_";
    if( $("#display").val() == 'PERU')
        id += "pr_";
    id += item;
    $(id).css({"color":"#EE5102","fontWeight":"bold"});
}


function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}


function getURLBeforeContext(){
    return window.protocol+'//'+window.host;
}

function openWindos(mesaje){

    var html = '<table class="tableEdit" style="background:none;margin: 10px 0% 0% 20%;">' +
        '<tr><td style="text-align: center" ><label>'+mesaje+'</label></td></tr>' +
        '<tr><td><br/></td></tr>' +
        '<tr><td style="text-align: center"><input type="submit" value="Accept" onclick="$.window.closeAll();" /></td></tr>' +
        '</table>';

    $.window({
        showModal: true,
        title: 'Notice',
        modalOpacity: 0,
        content: html,
        width: 250,
        height: 150,
        resizable: false,
        maximizable: false,
        minimizable: false
    });
}

function clearForm(){
    $(':input','#formEdit')
        .not(':button, :submit, :reset, :hidden')
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');
}
